package oneplay.YonghongBITools.Lineage;

import oneplay.YonghongBITools.YHWebAPI;
import oneplay.YonghongBITools.utils.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    private final static Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        if (args.length < 3) {
            System.out.println("Usage: username password path/of/report | -r path/of/folder(starts with no slash)");
            System.exit(1);
        }
        YHWebAPI yhWebAPI = new YHWebAPI(PropertiesUtil.getProperty("YH_URL_API"));
        try {
            yhWebAPI.login(args[0], args[1]);
            if ("-r".equals(args[2]) && args.length > 3) {
                for (String path : yhWebAPI.getReportList(args[3], true)) {
                    System.out.println(path + ": ");
                    YHLineageParser yhLineageParser = new YHLineageParser(yhWebAPI.getReportLineage(path));
                    String resultJson = yhLineageParser.getLineage();
                    System.out.println(resultJson + "\n");
                }
            } else {
                YHLineageParser yhLineageParser = new YHLineageParser(yhWebAPI.getReportLineage(args[2]));
                String resultJson = yhLineageParser.getLineage();
                System.out.println(resultJson);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            yhWebAPI.logout();
        }
    }

}
